package com.ruoyi.framework.mybatisplus;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.Data;

import java.util.List;
import java.util.function.Function;

@Data
public class PageDTO<R> {
    /** 总记录数 */
    private long total;
    private List<R> records;


    private PageDTO() {}

    public static <T, R> PageDTO<R> of(IPage<T> page, Function<T, R> mapper) {
        PageDTO<R> pageDTO = new PageDTO<>();
        pageDTO.total = page.getTotal();
        pageDTO.records = page.getRecords().stream()
                .map(mapper)
                .toList();
        return pageDTO;
    }

    public TableDataInfo toTableDataInfo() {
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(this.total);
        tableDataInfo.setRows(this.records);
        tableDataInfo.setCode(HttpStatus.SUCCESS); // Assuming 200 is the success code
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
